package com.aizhixin.lab.config;

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurerAdapter {
    @Autowired
    private Config config;

    @Bean
    public Docket baseApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("基础服务相关API")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/base/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("基础服务相关API")
                        .build());
    }
    @Bean
    public Docket resourceApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("资源管理API")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/resource/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("资源管理API")
                        .build());
    }
    
    @Bean
    public Docket initApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("初始化服务API")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/init/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("初始化服务API")
                        .build());
    }

    @Bean
    public Docket teacherApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("老师管理API")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/teacher/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("老师管理API")
                        .build());
    }

    @Bean
    public Docket courseApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("课程相关API")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/course/.*")))
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("课程相关API")
                        .build());
    }

    @Bean
    public Docket docsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("帮助文档相关API")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/docs/.*")))
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("帮助文档相关API")
                        .build());
    }
    
    @Bean
    public Docket taskApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("任务相关API")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/task/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("初始化服务API")
                        .build());
    }
    @Bean
    public Docket dockerApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("镜像相关API")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/docker/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("docker服务API")
                        .build());
    }
    @Bean
    public Docket statisticsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("统计相关API")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/statistics/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("统计服务API")
                        .build());
    }
    @Bean
    public Docket jsRunApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("jsRunAPI")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/jsrun/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("jsRunAPI")
                        .build());
    }

    @Bean
    public Docket LoginApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("登录API")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/user/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("登录API")
                        .build());
    }

    @Bean
    public Docket projectApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("项目模块Api")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/project/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("项目模块Api")
                        .build());
    }
    @Bean
    public Docket postApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("岗位模块Api")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/post/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("岗位模块Api")
                        .build());
    }

    @Bean
    public Docket jobApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("定时任务接口")
                .pathMapping("/")
                .select()
                .paths(or(regex("/api/v1/sys/quartzJob/.*")))//过滤的接口
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .version(config.getSysVersion())
                        .title("定时任务接口")
                        .build());
    }

    //添加ResourceHandler
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}
